package top.malaoshi.config;


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import top.malaoshi.model.JsonResult;

import java.io.IOException;

@ControllerAdvice
public class GlobalExceptionHandler {
    //当Controller抛出Exception异常时，由该方法处理
//    @ExceptionHandler({ArrayIndexOutOfBoundsException.class, IOException.class})
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResult handlerException(Exception e) {
        JsonResult res=new JsonResult(500,"服务器发生错误！");
        return res;
    }


    //当Controller抛出RuntimeException异常时，由该方法处理
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public JsonResult handlerException() {
        JsonResult res=new JsonResult(600,"服务器发生错误！");
        return res;
    }

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public JsonResult handlerThrowable() {
        JsonResult res=new JsonResult(700,"服务器发生错误！");
        return res;
    }
}
